package baiwa.rta.action.user;

import static com.opensymphony.xwork2.Action.SUCCESS;

import java.util.LinkedHashMap;

import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Namespaces;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.json.annotations.JSON;


import baiwa.rta.action.BaseAction;
import baiwa.rta.model.DataTableModel;
import baiwa.rta.model.JQueryDataTableParamModel;
import baiwa.rta.model.Role;
import baiwa.rta.service.RoleService;
import baiwa.rta.utils.DataTablesParamUtility;

@Namespaces(value = { @Namespace("/role") })
@ParentPackage(value = "json-default")
public class ROL001JsonAction extends BaseAction {

	private static final Logger logger = Logger.getLogger(ROL001JsonAction.class);
	private LinkedHashMap<String, Object> jsonData = new LinkedHashMap<>();
	private JQueryDataTableParamModel<Role> tableRoleModel = null;
	private Role role = null;
	private RoleService roleService = lookupBean(RoleService.class, "RoleService");
		

	public String execute() throws Exception {
		logger.info("ROL001JsonAction Home.");
		return SUCCESS;
	}

	@Action(value = "getAllRole", results = { @Result(name = SUCCESS, type = "json", params = { "root", "jsonData" }) })
	public String getAllRole() throws Exception {
		logger.info("ROL001JsonAction execute lists >>>> ROL001: "+ToStringBuilder.reflectionToString(role));
		
		jsonData.clear();
		tableRoleModel = DataTablesParamUtility.getParam(getRequest());
		
		DataTableModel<Role> dataTableModel = roleService.getRoleDataTable(
				role, 
				tableRoleModel.iDisplayStart,
				tableRoleModel.iDisplayLength
			);
		
		if (null != dataTableModel) {
			jsonData.putAll(tableRoleModel.toJSON(dataTableModel));
		}
		
		
		return SUCCESS;
	}

	@JSON(name = "jsonData")
	public LinkedHashMap<String, Object> getJsonData() {
		return jsonData;
	}

	public Role getRole() {
		return role;
	}

	public void setRole(Role role) {
		this.role = role;
	}

	@JSON(name = "tableRoleModel")
	public JQueryDataTableParamModel<Role> getTableRoleModel() {
		return tableRoleModel;
	}

	public void setTableRoleModel(JQueryDataTableParamModel<Role> tableRoleModel) {
		this.tableRoleModel = tableRoleModel;
	}

	

}
